﻿var jst = require('jst');
var fs = require('fs');
var markdown = require('node-markdown').Markdown;


jst.addFilter('inc', function(src){
	var txt = fs.readFileSync(src.tpl,'utf8');	
	if(src.type=='md'){
		txt = markdown(txt,false,'p');
		txt = txt.replace(new RegExp('<p>.<',''), '<');
		txt = txt.replace(new RegExp('<p><','g'), '<');
		txt = txt.replace(new RegExp('></p>','g'), '>');
	}
	
	txt = jst.render(txt, src.param);	
	return txt;
});
jst.addFilter('imp', function(f){
	return function(){
		var txt = fs.readFileSync(f,'utf8');
		return jst.render(txt, {});	
	}
});
function dump(f, dir){
	return function(err,ctx){
		console.log(err);
		fs.writeFileSync(f,ctx,'utf8');
		fs.writeFileSync('../web'+dir+'/'+f, ctx, 'utf8');
	}
}
	
jst.renderFile('page.htm',
	{
		title:'制作你的独立游戏美术资源',
		body:{
			type:'md',
			tpl:'indie-game-art1.htm',
			param:{}
		},
		article:'active'
	},
	dump('indie-game-art1.html', '/article'));

jst.renderFile('page.htm',
	{
		title:'第一次做美术资源的经验',
		body:{
			type:'md',
			tpl:'indie-game-art2.htm',
			param:{}
		},
		article:'active'
	},
	dump('indie-game-art2.html', '/article'));
	
jst.renderFile('page.htm',
	{
		title:'所有文章',
		body:{
			tpl:'article-index.htm',
			param:{}
		},
		article:'active'
	},
	dump('index.html', '/article'));
	
jst.renderFile('page.htm',
	{
		title:'坦克1V3',
		body:{
			tpl:'tank-index.htm',
			param:{}
		},
		game:'active'
	},
	dump('index.html', '/tank1v3'));
